Page({
  onShareAppMessage() {
    return {
      title: '书语',
      path: 'pages/index/index'
    }
  },
  data: {
    artcileArray: [],
    indicatorDots: false,
    vertical: false,
    autoplay: false,
    interval: 2000,
    duration: 500,
    skip:0,
    allCount:null,
    current:0
  },

  changeIndicatorDots() {
    this.setData({
      indicatorDots: !this.data.indicatorDots
    })
  },

  changeAutoplay() {
    this.setData({
      autoplay: !this.data.autoplay
    })
  },
  intervalChange(e) {
    this.setData({
      interval: e.detail.value
    })
  },
  durationChange(e) {
    this.setData({
      duration: e.detail.value
    })
  },
  async getArtcileCount(){
    await wx.cloud.callFunction({
      name:"getArtcileCount"
    }).then(async res=>{
    this.setData({
      allCount:res.result.total
    })
    this.setData({
      skip:parseInt(Math.random()*(this.data.allCount-10))
    })
    })
  },
  async randomArtcile(){
  let randomA=parseInt(Math.random()*((this.data.allCount-10)-this.data.skip)+this.data.skip);
  if(this.data.skip<this.data.allCount){
    if(randomA-this.data.skip<10){
        this.setData({
          skip:parseInt(randomA+10)
        })
    }else{
      this.setData({
        skip:parseInt(randomA)
      })
    }
  }else{
    this.setData({
      skip:parseInt(Math.random()*(this.data.allCount-10))
    })
  }
  },
 async getArtcile(){
   await this.randomArtcile();
   await wx.cloud.callFunction({
    name:"getArtcile",
    data:{
      skip:this.data.skip
    }
  }).then(async res=>{
    if(res.result){
      this.setData({
        artcileArray:res.result.data,
        current:0
      })
    }
    while(this.data.artcileArray.length<1){
      await this.getArtcile();
    }
  })
},
 async watchCurrent(e){
  if(e.detail.current==this.data.artcileArray.length-1){
   await this.getArtcile();
      wx.showToast({
        title: '正在为您搜索',
        icon:"loading"
      })
  }
  },
 async onLoad(){
  await this.getArtcileCount();
  await this.getArtcile();
  },
  async onPullDownRefresh(){
    await this.getArtcile();
    wx.stopPullDownRefresh();
  }
})